using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Code5Systems.FormMaster.Business;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;

namespace Code5Systems.FormMaster
{
	
	public partial class FormMasterEdit : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		
#region Page and Design Event Handlers
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			try
			{
				HtmlGenericControl head = this.Page.FindControl("Head") as HtmlGenericControl;
				if (head != null)
				{
					HtmlGenericControl scriptTag = new HtmlGenericControl("script");
					scriptTag.Attributes.Add("type", "text/javascript");
					scriptTag.Attributes.Add("src", String.Format("{0}DesktopModules/Code5Systems_FormMaster/assets/js/jquery-calx-1.1.8.js", Utility.GetAppPath()));
					head.Controls.Add(scriptTag);
				}

				if (Utility.SessionItemExists("PreviewMode"))
				{
					_previewMode = System.Convert.ToBoolean(Utility.GetSessionItem<bool>("PreviewMode"));
					this.lstMode.SelectedIndex = System.Convert.ToInt32(_previewMode ? 0 : 1);
				}
				
				if (_previewMode)
				{
					this.cmdUpdate.Enabled = false;
					this.cmdResetForm.Enabled = false;
				}
				else
				{
					this.cmdUpdate.Enabled = true;
					this.cmdResetForm.Enabled = true;
				}
				
				UserController objUsers = new UserController();
				UserInfo objUser = UserController.GetCurrentUserInfo();
				
				if (!(objUser.IsSuperUser || DotNetNuke.Security.PortalSecurity.IsInRole("Administrators")))
				{
					canEditQuery = false;
				}
				
				this.LoaderCtl = (PortalModuleBase) this.Parent.Parent;
				
				if (!Page.IsPostBack)
				{
					AddInsertAtCursorScript();
					AddEditStyles();
				}
				
				//AddScriptVars()
				
				//PopulatePagesDropdown()
				
				strOperationSuccess = Localization.GetString("OperationSuccess", this.LocalResourceFile);
				objFormSettings = Utility.GetFormSettings(ModuleId);
				objFormCtl = new Code5Systems.FormMaster.Business.FormController(ModuleId);
				objFormInfo = objFormCtl.FormInfo;
				
				if (!int.TryParse(Request.Form["__EVENTARGUMENT"], out _curPage))
				{
					if (!int.TryParse(this.lstFormPage.SelectedValue, out _curPage))
					{
						int.TryParse(this.currentPage.Value, out _curPage);
						if (_curPage == 0)
						{
							_curPage = 1;
						}
					}
					
				}
				
				objFormInfo.CurrentPage = objFormCtl.Pages.Find(p => p.PageNum == _curPage);
				_controlCount = Code5Systems.FormMaster.Business.FormController.GetControlCount(objFormInfo);
				
				if (objFormCtl.Pages.Count == 0)
				{
					//xDoc = XDocument.Load(Server.MapPath(Me.ControlPath & "assets/xml/BaseForm.xml"))
					this.xDoc = XDocument.Parse((string) (AddFormPage(1).PageData));
					SaveFormDefinition();
				}
				else
				{
					this.xDoc = XDocument.Parse(objFormInfo.CurrentPage.PageData);
				}
				
				this.cmdResetForm.Attributes.Add("onclick", "javascript:return confirm(\'" + Localization.GetString("ResetFormMessage", LocalResourceFile) + "\');");
				
				if (!(ViewState["ControlLocation"] == null))
				{
					strLocation = System.Convert.ToString(ViewState["ControlLocation"]);
					oCtlLoc = Utility.GetControlLocation(strLocation);
				}
				
				if (!Page.IsPostBack)
				{
					BindDesignForm();
					this.ctlImage.FileFilter = DotNetNuke.Common.Globals.glbImageFileTypes;
					//CreateForm()
					//CreateMoveTo()
				}
				else
				{
					//CreateForm()
				}
				
				CreateForm();
				CreateMoveTo();
				
				if (DotNetNuke.Framework.AJAX.IsEnabled())
				{
					//DotNetNuke.Framework.AJAX.RegisterPostBackControl(Me.cmdInsDelRowCol)
					//DotNetNuke.Framework.AJAX.RegisterPostBackControl(Me.cmdUpdate)
					//DotNetNuke.Framework.AJAX.RegisterPostBackControl(Me.lstFormPage)
					
				}
				else
				{
					bAjaxDisabled = true;
				}
				
				for (int i = 0; i <= 200; i++)
				{
					this.lstNumToInsert.Items.Add(i.ToString());
					this.lstNumToDelete.Items.Add(i.ToString());
				}
			}
			catch (HttpException webEx)
			{
				if (!webEx.Message.StartsWith("Failed to load viewstate."))
				{
					DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, webEx.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
					DotNetNuke.Services.Exceptions.Exceptions.LogException(webEx);
				}
			}
			catch (Exception exc)
			{
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, exc.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
				DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
			}
			
			DotNetNuke.Common.Globals.SetFormFocus(this.txtLabel);
			
		}
		
		//Update for the main form values, not the individulal Form Controls
		protected void cmdUpdate_Click(object sender, EventArgs e)
		{
			try
			{
				if (Page.IsValid)
				{
					bool bNewForm = false;
					
					objFormInfo = objFormCtl.FormInfo;
					objFormInfo.LastModifiedByID = objFormInfo.CreatedByID;
					objFormInfo.CurrentPage.TableColumns = Convert.ToByte(this.lstTableColumns.SelectedValue);
					objFormInfo.CurrentPage.TableRows = Convert.ToByte(this.lstTableRows.SelectedValue);
					objFormInfo.CurrentPage.LabelPosition = Convert.ToByte(this.lstLabelPosition.SelectedValue);
					objFormInfo.CurrentPage.PageData = this.xDoc.ToString();
					
					if (ValidateTable(objFormInfo))
					{
						objFormCtl.Update(objFormInfo);
						
						objFormInfo.CurrentPage.TableColumns = byte.Parse(this.lstTableColumns.SelectedValue);
						objFormInfo.CurrentPage.TableRows = byte.Parse(this.lstTableRows.SelectedValue);
						objFormInfo.CurrentPage.LabelPosition = byte.Parse(this.lstLabelPosition.SelectedValue);
						objFormInfo.CurrentPage.ShortName = this.txtPageShortName.Text;
						objFormInfo.CurrentPage.Title = this.txtPageTitle.Text;
						objFormInfo.CurrentPage.UseTitle = this.chkUseTitle.Checked;
						//objFormInfo.CurrentPage.PageData = Me.xDoc.ToString()
						Code5Systems.FormMaster.Business.FormPageController oPageCtlr = new Code5Systems.FormMaster.Business.FormPageController();
						oPageCtlr.UpdateFormPage(objFormInfo.CurrentPage);
						objFormCtl.Pages.Remove(objFormInfo.CurrentPage);
						objFormCtl.Pages.Add(objFormInfo.CurrentPage);
						
						BindDesignForm();
						CreateForm();
						CreateMoveTo();
					}
					
				}
				
			}
			catch (Exception exc)
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		
		//Add a new control to the form
		protected void cmdAdd_Click(object sender, System.Web.UI.WebControls.CommandEventArgs e)
		{
			
			strLocation = System.Convert.ToString(e.CommandArgument);
			
			ViewState.Add("ControlLocation", strLocation);
			
			oCtlLoc = Utility.GetControlLocation(strLocation);
			
			BindControlForm();
		}
		
		//Edit a Form Control
		protected void cmdEdit_Click(object sender, System.Web.UI.WebControls.CommandEventArgs e)
		{
			strLocation = System.Convert.ToString(e.CommandArgument);
			ViewState.Add("ControlLocation", strLocation);
			oCtlLoc = Utility.GetControlLocation(strLocation);
			BindControlForm();
		}
		
		private string m_fieldToFind = string.Empty;
		//Delete a form control
		protected void cmdDelete_Click(object sender, System.Web.UI.WebControls.CommandEventArgs e)
		{
			string strLocation = System.Convert.ToString(e.CommandArgument);
			
			oCtlLoc = Utility.GetControlLocation(strLocation);
			
			try
			{
				List<Code5Systems.FormMaster.Business.FormFieldRulesInfo> oFieldRules = default(List<Code5Systems.FormMaster.Business.FormFieldRulesInfo>);
				Code5Systems.FormMaster.Business.FormControlInfo oFormCtlInfo = this.GetFormControlInfo((oCtlLoc.Row).ToString(), (oCtlLoc.Col).ToString());
				m_fieldToFind = oFormCtlInfo.ID;
				
				//Make sure the field is not used in a Field rule
				Code5Systems.FormMaster.Business.FormFieldRulesController oFieldRulesCtl = new Code5Systems.FormMaster.Business.FormFieldRulesController();
				oFieldRules = oFieldRulesCtl.GetByFormGeneric(this.objFormInfo.FormID);
				if (oFieldRules.Exists(FindRuleField))
				{
					throw (new Exception(Localization.GetString("RuleFieldDeleteViolation", this.LocalResourceFile)));
				}
				
				//Make sure the field is not mapped to an external table
				Code5Systems.FormMaster.Business.FormMappingController oMapCtl = new Code5Systems.FormMaster.Business.FormMappingController();
				List<Code5Systems.FormMaster.Business.FormMappingInfo> oFieldMaps = default(List<Code5Systems.FormMaster.Business.FormMappingInfo>);
				oFieldMaps = oMapCtl.GetByFormGeneric(this.objFormInfo.FormID);
				if (oFieldMaps.Exists(FindFieldMapping))
				{
					throw (new Exception(Localization.GetString("FieldMapDeleteViolation", this.LocalResourceFile)));
				}
				
				//Make sure this field is not being used in a cascading dropdown set
				if (oFormCtlInfo.IsParentList || oFormCtlInfo.IsChildList)
				{
					throw (new Exception(Localization.GetString("FieldCascadeDeleteViolation", this.LocalResourceFile)));
				}
				objFormCtl.DeleteControl(this.xDoc, (oCtlLoc.Row).ToString(), (oCtlLoc.Col).ToString());
				
				this.SaveFormDefinition();
				
				BindDesignForm();
				
				CreateForm();
				CreateMoveTo();
				
			}
			catch (Exception ex)
			{
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, ex.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
			}
			
			
		}
		
		private bool FindFieldMapping(Code5Systems.FormMaster.Business.FormMappingInfo mapping)
		{
			bool bFound = false;
			if (mapping.FormField == m_fieldToFind)
			{
				bFound = true;
			}
			return bFound;
		}
		
		private bool FindRuleField(Code5Systems.FormMaster.Business.FormFieldRulesInfo rule)
		{
			bool bFound = false;
			
			if (rule.SourceField == m_fieldToFind)
			{
				bFound = true;
			}
			
			if (bFound == false && rule.FormFields.Contains(m_fieldToFind))
			{
				bFound = true;
			}
			
			return bFound;
		}
		
		//Move a form control from one cell to another
		protected void cmdMove_Click(object sender, System.Web.UI.WebControls.CommandEventArgs e)
		{
			string[] strCommands = this.hdnMoveFrom.Value.Split('|');
			string strMoveFrom = strCommands[0];
			ControlLoaction oMoveSource = Utility.GetControlLocation(strMoveFrom);
			strLocation = System.Convert.ToString(e.CommandArgument);
			oCtlLoc = Utility.GetControlLocation(strLocation);
			
			if (strCommands[1] == "m")
			{
				objFormCtl.MoveControl(this.xDoc, objFormInfo, oMoveSource, oCtlLoc);
			}
			else
			{
				objFormCtl.CopyControl(this.xDoc, objFormInfo, oMoveSource, oCtlLoc);
			}
			
			this.SaveFormDefinition();
			
			bool doRedirect = false;
			if (System.Convert.ToInt32(this.objFormInfo.CurrentPage.TableColumns) * System.Convert.ToInt32(objFormInfo.CurrentPage.TableRows) > 500)
			{
				doRedirect = true;
			}

			if (doRedirect)
			{
				Response.Redirect(Request.Url.AbsoluteUri);
			}
			
			BindDesignForm();
			
			CreateForm();
			CreateMoveTo();
			
		}
		
		protected void cmdInsDelRowCol_Click(object sender, System.EventArgs e)
		{
			ControlLoaction oManagerLoc = default(ControlLoaction);
			
			oManagerLoc = new ControlLoaction(this.hdnManagerLoc.Value);
			if (oManagerLoc.Row == 0)
			{
				ColumnMgr(oManagerLoc);
			}
			else
			{
				RowManager(oManagerLoc);
			}
			
			//Utility.ClearSessionItem("ManagerLoc")
			
			bool doRedirect = false;
			if (System.Convert.ToInt32(this.objFormInfo.CurrentPage.TableColumns) * System.Convert.ToInt32(objFormInfo.CurrentPage.TableRows) > 500)
			{
				doRedirect = true;
			}
			
			if (doRedirect)
			{
				Response.Redirect(Request.Url.AbsoluteUri);
			}
			
			BindDesignForm();
			
			CreateForm();
			CreateMoveTo();
		}
		
		private void ColumnMgr(ControlLoaction loc)
		{
			XElement xControl = default(XElement);
			XAttribute xColAttr = default(XAttribute);
			int iRowLoc = default(int);
			int iColLoc = default(int);
			int iColOffset = default(int);
			int iFromValue = default(int);
			int iToValue = default(int);
			short iStepValue = default(short);
			ControlLoaction ctrlLoc = new ControlLoaction();
			
			tAry = this.objFormCtl.GetTableMatrix(objFormInfo, objFormInfo.CurrentPage.PageNum);
			ctrlLoc.Page = objFormInfo.CurrentPage.PageNum;

			if (this.rdoInsert.Checked)
			{
				iColOffset = int.Parse(this.lstNumToInsert.SelectedValue);
				iFromValue = this.objFormInfo.CurrentPage.TableColumns;
				iToValue = loc.Col;
				iStepValue = (short) (-1);

				for (int iCol = iFromValue; iCol >= iToValue; iCol += iStepValue)
				{
					iColLoc = iCol;
					for (int iRow = 1; iRow <= objFormInfo.CurrentPage.TableRows; iRow++)
					{
						ctrlLoc.Row = iRow;
						iRowLoc = iRow;
						if (!tAry[iRow - 1, iCol - 1].IsEmpty)
						{
							xControl = (from c in xDoc.Root.Elements("Controls").Elements("Control") where System.Convert.ToInt32(c.Attributes("TableRow").First().Value) == iRowLoc && System.Convert.ToInt32(c.Attributes("TableCol").First().Value) == iColLoc select c).FirstOrDefault();

							xColAttr = xControl.Attribute("TableCol");
							if (xColAttr != null)
							{
								xColAttr.Value = (iCol + iColOffset).ToString();
								ctrlLoc.Col = int.Parse(xColAttr.Value);
								objFormCtl.SetValidatorControlsPosition(xControl, ctrlLoc);
							}
						}
					}
				}
			}
			else
			{
				iColOffset = int.Parse(this.lstNumToDelete.SelectedValue) * -1;
				iFromValue = loc.Col;
				iToValue = this.objFormInfo.CurrentPage.TableColumns;
				iStepValue = (short) 1;

				for (int iCol = iFromValue; iCol <= iToValue; iCol += iStepValue)
				{
					iColLoc = iCol;
					for (int iRow = 1; iRow <= objFormInfo.CurrentPage.TableRows; iRow++)
					{
						ctrlLoc.Row = iRow;
						iRowLoc = iRow;
						if (!tAry[iRow - 1, iCol - 1].IsEmpty)
						{
							xControl = (from c in xDoc.Root.Elements("Controls").Elements("Control") where System.Convert.ToInt32(c.Attributes("TableRow").First().Value) == iRowLoc && System.Convert.ToInt32(c.Attributes("TableCol").First().Value) == iColLoc select c).FirstOrDefault();

							xColAttr = xControl.Attribute("TableCol");
							if (xColAttr != null)
							{
								xColAttr.Value = (iCol + iColOffset).ToString();
								ctrlLoc.Col = int.Parse(xColAttr.Value);
								objFormCtl.SetValidatorControlsPosition(xControl, ctrlLoc);
							}
						}
					}
				}
			}
			
			
			
			this.objFormInfo.CurrentPage.TableColumns = System.Convert.ToByte(this.objFormInfo.CurrentPage.TableColumns + iColOffset <= 0 ? 1 : this.objFormInfo.CurrentPage.TableColumns + iColOffset);
			this.lstTableColumns.SelectedIndex = this.lstTableColumns.Items.IndexOf(this.lstTableColumns.Items.FindByValue(objFormInfo.CurrentPage.TableColumns.ToString()));
			this.SaveFormDefinition();
			this.objFormCtl.Update(this.objFormInfo);
			
		}
		
		private void RowManager(ControlLoaction loc)
		{
			XElement xControl = default(XElement);
			XAttribute xRowAttr = default(XAttribute);
			int iRowLoc = default(int);
			int iColLoc = default(int);
			int iRowOffset = default(int);
			int iFromValue = default(int);
			int iToValue = default(int);
			short iStepValue = default(short);
			ControlLoaction ctrlLoc = new ControlLoaction();
			
			tAry = this.objFormCtl.GetTableMatrix(objFormInfo, objFormInfo.CurrentPage.PageNum);
			ctrlLoc.Page = objFormInfo.CurrentPage.PageNum;
			
			if (this.rdoInsert.Checked)
			{
				iRowOffset = int.Parse(this.lstNumToInsert.SelectedValue);
				iFromValue = this.objFormInfo.CurrentPage.TableRows;
				iToValue = loc.Row;
				iStepValue = (short) (-1);

				for (int iRow = iFromValue; iRow >= iToValue; iRow += iStepValue)
				{
					iRowLoc = iRow;
					for (int iCol = 1; iCol <= objFormInfo.CurrentPage.TableColumns; iCol++)
					{
						ctrlLoc.Col = iCol;
						iColLoc = iCol;
						if (!tAry[iRow - 1, iCol - 1].IsEmpty)
						{
							xControl = (from c in xDoc.Root.Elements("Controls").Elements("Control") where System.Convert.ToInt32(c.Attributes("TableRow").First().Value) == iRowLoc && System.Convert.ToInt32(c.Attributes("TableCol").First().Value) == iColLoc select c).FirstOrDefault();
							xRowAttr = xControl.Attribute("TableRow");
							if (xRowAttr != null)
							{
								xRowAttr.Value = (iRow + iRowOffset).ToString();
								ctrlLoc.Row = int.Parse(xRowAttr.Value);
								objFormCtl.SetValidatorControlsPosition(xControl, ctrlLoc);
							}
						}
					}
				}
			}
			else
			{
				iRowOffset = int.Parse(this.lstNumToDelete.SelectedValue) * -1;
				iFromValue = loc.Row;
				iToValue = this.objFormInfo.CurrentPage.TableRows;
				iStepValue = (short) 1;

				for (int iRow = iFromValue; iRow <= iToValue; iRow += iStepValue)
				{
					iRowLoc = iRow;
					for (int iCol = 1; iCol <= objFormInfo.CurrentPage.TableColumns; iCol++)
					{
						ctrlLoc.Col = iCol;
						iColLoc = iCol;
						if (!tAry[iRow - 1, iCol - 1].IsEmpty)
						{
							xControl = (from c in xDoc.Root.Elements("Controls").Elements("Control") where System.Convert.ToInt32(c.Attributes("TableRow").First().Value) == iRowLoc && System.Convert.ToInt32(c.Attributes("TableCol").First().Value) == iColLoc select c).FirstOrDefault();
							xRowAttr = xControl.Attribute("TableRow");
							if (xRowAttr != null)
							{
								xRowAttr.Value = (iRow + iRowOffset).ToString();
								ctrlLoc.Row = int.Parse(xRowAttr.Value);
								objFormCtl.SetValidatorControlsPosition(xControl, ctrlLoc);
							}
						}
					}
				}
			}
			
			
			
			this.objFormInfo.CurrentPage.TableRows = System.Convert.ToByte(this.objFormInfo.CurrentPage.TableRows + iRowOffset <= 0 ? 1 : this.objFormInfo.CurrentPage.TableRows + iRowOffset);
			this.lstTableRows.SelectedIndex = this.lstTableRows.Items.IndexOf(this.lstTableRows.Items.FindByValue(this.objFormInfo.CurrentPage.TableRows.ToString()));
			this.SaveFormDefinition();
			this.objFormCtl.Update(this.objFormInfo);
			
		}
		
		protected void ColSpanHandler(System.Object sender, System.EventArgs e)
		{
			string[] valArray = null;
			string col = default(string);
			string row = default(string);
			string colSpan = default(string);
			
			DropDownList lstColSpan = (DropDownList) sender;
			if (lstColSpan != null)
			{
				valArray = lstColSpan.SelectedValue.Split(';');
				col = valArray[0];
				row = valArray[1];
				colSpan = valArray[2];
				
				objFormCtl.UpdateColSpan(this.xDoc, row, col, colSpan);
				
				this.SaveFormDefinition();
				
				//Response.Redirect(Request.RawUrl)
				BindDesignForm();
				CreateForm();
				CreateMoveTo();
				
			}
		}
		
		protected void RowSpanHandler(System.Object sender, System.EventArgs e)
		{
			string[] valArray = null;
			string col = default(string);
			string row = default(string);
			string rowSpan = default(string);
			
			DropDownList lstRowSpan = (DropDownList) sender;
			if (lstRowSpan != null)
			{
				valArray = lstRowSpan.SelectedValue.Split(';');
				col = valArray[0];
				row = valArray[1];
				rowSpan = valArray[2];
				
				objFormCtl.UpdateRowSpan(this.xDoc, row, col, rowSpan);
				
				this.SaveFormDefinition();
				
				//Response.Redirect(Request.RawUrl)
				BindDesignForm();
				CreateForm();
				CreateMoveTo();
				
			}
		}
		
		protected void TabIndexHandler(System.Object sender, System.EventArgs e)
		{
			string[] valArray = null;
			string col = default(string);
			string row = default(string);
			string tabIndex = default(string);
			
			DropDownList lstTabIndex = (DropDownList) sender;
			if (lstTabIndex != null)
			{
				valArray = lstTabIndex.SelectedValue.Split(';');
				col = valArray[0];
				row = valArray[1];
				tabIndex = valArray[2];
				
				objFormCtl.UpdateTabIndex(this.xDoc, row, col, tabIndex);
				
				this.SaveFormDefinition();
				
				//Response.Redirect(Request.RawUrl)
				BindDesignForm();
				CreateForm();
				CreateMoveTo();
				
			}
		}
		
		private bool IsColmunEmpty(int col)
		{
			return !usedCols.Contains(col);
		}
		
		private bool IsRowEmpty(int row)
		{
			return !usedRows.Contains(row);
		}
		
		//Clears the entire form definition
		protected void cmdResetForm_Click(System.Object sender, System.EventArgs e)
		{
			this.objFormCtl.ResetForm(this.xDoc);
			this.SaveFormDefinition();
			this.lstTableColumns.SelectedIndex = 0;
			this.lstTableRows.SelectedIndex = 0;
			BindDesignForm();
			//CreateForm()
		}

		protected void lstMode_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			_previewMode = this.lstMode.SelectedIndex == 0 ? true : false;
			if (_previewMode)
			{
				this.cmdUpdate.Enabled = false;
				this.cmdResetForm.Enabled = false;
				CreateForm();
			}
			else
			{
				this.cmdUpdate.Enabled = true;
				this.cmdResetForm.Enabled = true;
				this.BindDesignForm();
			}
		}
		
		protected void lstFormPage_SelectedIndexChanged(object sender, EventArgs e)
		{
			int curPage = default(int);
			int.TryParse(this.currentPage.Value, out curPage);
			
			try
			{
				switch (this.lstFormPage.SelectedValue)
				{
					case "new":
						//Add New Page
						int newPage_1 = this.lstFormPage.Items.Count - 2;
						objFormInfo.CurrentPage = AddFormPage(newPage_1);
						this.currentPage.Value = (newPage_1).ToString();
						BindDesignForm();
						CreateForm();
						CreateMoveTo();
						break;
						
					case "copy":
						int newPage = this.lstFormPage.Items.Count - 2;
						objFormInfo.CurrentPage = CopyFormPage(newPage);
						this.currentPage.Value = (newPage).ToString();
						BindDesignForm();
						CreateForm();
						CreateMoveTo();
						break;
						
					case "delete":
						//Delete Page
						Code5Systems.FormMaster.Business.FormPageController pageCtlr = new Code5Systems.FormMaster.Business.FormPageController();
						pageCtlr.DeleteFormPageByPageNum(objFormInfo.FormID, int.Parse(this.currentPage.Value));
						objFormCtl.Pages.Remove(objFormCtl.Pages.Find(p => p.PageNum == curPage));
						objFormCtl.VerifyPageNumbers();
						
						if (curPage > 1)
						{
							this.currentPage.Value = (curPage - 1).ToString();
							_curPage = curPage - 1;
						}
						else
						{
							this.currentPage.Value = "1";
							_curPage = 1;
						}
						objFormInfo.CurrentPage = objFormCtl.Pages.Find(p => p.PageNum == _curPage);
						BindDesignForm();
						CreateForm();
						CreateMoveTo();
						break;
						
					default:
						//Change Page
						this.currentPage.Value = this.lstFormPage.SelectedValue;
						BindDesignForm();
						CreateForm();
						CreateMoveTo();
						break;
				}
			}
			catch (Exception ex)
			{
				string msg = ex.Message;
			}
			
		}
		
#endregion
		
#region Control Form Event Handlers
		
		//Handles the individual Control Updates
		protected void cmdControlUpdate_Click(System.Object sender, System.EventArgs e)
		{
			Code5Systems.FormMaster.Business.FormControlInfo objFormControlInfo = default(Code5Systems.FormMaster.Business.FormControlInfo);
			string parentID = string.Empty;
			
			try
			{
				if (Page.IsValid)
				{
					if (!(ViewState["ControlLocation"] == null))
					{
						
						objFormControlInfo = this.GetFormControlInfo((oCtlLoc.Row).ToString(), (oCtlLoc.Col).ToString());
						
						if (objFormControlInfo == null)
						{
							objFormControlInfo = objFormCtl.AddControl(this.xDoc, int.Parse(lstType.SelectedItem.Value).ToEnum<ControlType>(), (oCtlLoc.Row).ToString(), (oCtlLoc.Col).ToString());
						}
						
						if (objFormControlInfo != null)
						{
							bool wasCascadingList = objFormControlInfo.CascadingList;
							parentID = objFormControlInfo.ParentControlID;
							
							FillFormControlInfo(objFormControlInfo);
							
							//Set the new values in the XmlDocument
							objFormCtl.UpdateControlValues(this.xDoc, objFormControlInfo);
							this.objFormCtl.FormInfo.CurrentPage.PageData = this.xDoc.ToString();
							
							if (objFormControlInfo.Type == Code5Systems.FormMaster.Business.ControlType.DropDownList && wasCascadingList != objFormControlInfo.CascadingList)
							{
								if (objFormControlInfo.CascadingList)
								{
									parentID = objFormControlInfo.ParentControlID;
								}
							}
							
							// If the updated Dropdown is or was a member of a Cascade Set do a Rule Fixup
							if (objFormControlInfo.Type == Code5Systems.FormMaster.Business.ControlType.DropDownList)
							{
								string strFields = string.Empty;
								System.Collections.Generic.List<Code5Systems.FormMaster.Business.FormControlInfo> cascadeMembers = objFormCtl.GetCascadeSet(parentID);
								Code5Systems.FormMaster.Business.FormControlInfo cascadeParent = cascadeMembers.Where(c => c.IsChildList == false).SingleOrDefault();
								
								if (cascadeParent != null)
								{
									Code5Systems.FormMaster.Business.FormFieldRulesController oFieldRulesCtl = new Code5Systems.FormMaster.Business.FormFieldRulesController();
									System.Collections.Generic.List<Code5Systems.FormMaster.Business.FormFieldRulesInfo> oFieldRules = oFieldRulesCtl.GetByForm(objFormCtl.FormInfo.FormID);
									Code5Systems.FormMaster.Business.FormFieldRulesInfo oFieldRuleTarget = oFieldRules.Where(r => r.FormFields.Contains(cascadeParent.ID)).SingleOrDefault();
									if (oFieldRuleTarget != null)
									{
										foreach (Code5Systems.FormMaster.Business.FormControlInfo ctrl in cascadeMembers)
										{
											strFields += ctrl.ID + ",";
										}
										strFields = strFields.Substring(0, strFields.Length - 1);
										oFieldRuleTarget.FormFields = strFields;
										oFieldRulesCtl.Delete(oFieldRuleTarget.FieldRuleID);
										oFieldRulesCtl.Add(oFieldRuleTarget);
									}
								}
							}
							
							//If the user changed the control type to a Checkbox, CheckboxList, Label or Submit Button, and there were
							//validators for the old control type, they must be removed because these controls
							//do not support validation.
							if (objFormControlInfo.Validators.Count > 0)
							{
								Code5Systems.FormMaster.Business.FormControlInfo objValidatorFormControlInfo = default(Code5Systems.FormMaster.Business.FormControlInfo);
								System.Collections.IEnumerator myEnumerator = objFormControlInfo.Validators.GetEnumerator();
								
								while (myEnumerator.MoveNext())
								{
									objValidatorFormControlInfo = (Code5Systems.FormMaster.Business.FormControlInfo) myEnumerator.Current;
									
									if (objFormControlInfo.Type == Code5Systems.FormMaster.Business.ControlType.CheckBox || objFormControlInfo.Type == Code5Systems.FormMaster.Business.ControlType.Label || objFormControlInfo.Type == Code5Systems.FormMaster.Business.ControlType.SubmitButton)
									{
										objFormCtl.DeleteValidatorControl(this.xDoc, objFormControlInfo, objValidatorFormControlInfo.Type);
									}
									
									if (objFormControlInfo.Type == Code5Systems.FormMaster.Business.ControlType.CheckBoxList)
									{
										if (objValidatorFormControlInfo.Type != Code5Systems.FormMaster.Business.ControlType.ChkListValidator)
										{
											objFormCtl.DeleteValidatorControl(this.xDoc, objFormControlInfo, objValidatorFormControlInfo.Type);
										}
									}
									
									objFormCtl.UpdateControlValues(this.xDoc, objValidatorFormControlInfo);
									
								}
							}
							
							SaveFormDefinition();
							BindDesignForm();
							CreateForm();
							CreateMoveTo();
						}
					}
				}
			}
			catch (System.Threading.ThreadAbortException)
			{
				
			}
			catch (Exception ex)
			{
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, ex.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
				DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
			}
		}

		protected void cmdCancel_Click(System.Object sender, System.EventArgs e)
		{
			BindDesignForm();
			//Response.Redirect(Request.RawUrl)
		}
		
		//Change the Control Type
		protected void lstType_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			try
			{
				if (this.lstType.SelectedValue.ToEnum<ControlType>() == Code5Systems.FormMaster.Business.ControlType.SubmitButton)
				{
					if (this.objFormCtl.Pages.Count > 1)
					{
						BindControlForm();
						throw (new Exception(Localization.GetString("SubmitButtonError", this.LocalResourceFile)));
					}
				}

				if (this.lstType.SelectedValue.ToEnum<ControlType>() == Code5Systems.FormMaster.Business.ControlType.FileUpload)
				{
					if (this._curPage < this.objFormCtl.Pages.Count)
					{
						BindControlForm();
						throw (new Exception(Localization.GetString("LabelUploadTypeWizardMsg", this.LocalResourceFile)));
					}
				}

				if (!(ViewState["ControlLocation"] == null))
				{
					Code5Systems.FormMaster.Business.FormControlInfo objFormControlInfo = this.GetFormControlInfo((oCtlLoc.Row).ToString(), (oCtlLoc.Col).ToString());
					
					if (objFormControlInfo != null)
					{
						
						if (objFormControlInfo.IsChildList || objFormControlInfo.IsParentList)
						{
							if (this.lstType.SelectedValue != (Code5Systems.FormMaster.Business.ControlType.DropDownList.ToString()))
							{
								throw (new Exception(Localization.GetString("FieldCascadeChangeTypeViolation", this.LocalResourceFile)));
							}
						}

						if (this.lstType.SelectedValue.ToEnum<ControlType>() == Code5Systems.FormMaster.Business.ControlType.DropDownList)
						{
							if (!this.ValidateDropdownList(objFormControlInfo, 1))
							{
								//Set the Control Type back to the original.
								this.lstType.SelectedIndex = this.lstType.Items.IndexOf(this.lstType.Items.FindByValue(
									objFormControlInfo.Type.EnumToInt<ControlType>().ToString()));
								return;
							}
						}

						if (this.lstType.SelectedValue.ToEnum<ControlType>() == Code5Systems.FormMaster.Business.ControlType.CheckBoxList)
						{
							if (objFormControlInfo.Validators.Count > 0)
							{
								Code5Systems.FormMaster.Business.FormControlInfo objValidatorFormControlInfo = default(Code5Systems.FormMaster.Business.FormControlInfo);
								System.Collections.IEnumerator myEnumerator = objFormControlInfo.Validators.GetEnumerator();
								
								while (myEnumerator.MoveNext())
								{
									objValidatorFormControlInfo = (Code5Systems.FormMaster.Business.FormControlInfo) myEnumerator.Current;
									if (objValidatorFormControlInfo.Type != Code5Systems.FormMaster.Business.ControlType.ChkListValidator)
									{
										DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, Localization.GetString("ChangeToCheckboxListOperation", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
										return;
									}
									
								}
							}
						}
					}

					bFormChange = true;
					
					BindControlForm();
					
				}
			}
			catch (Exception ex)
			{
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(_loaderCtl, ex.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
			}
			
			
		}
		
		//Toggle the Auto Fill From rows
		protected void lstAutoFillType_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			
			this.trQueryResults.Visible = false;

			Code5Systems.FormMaster.Business.FormController.AutoFillType type = int.Parse(this.lstAutoFillType.SelectedValue).ToEnum < Code5Systems.FormMaster.Business.FormController.AutoFillType>();
			
			switch (type)
			{
				case Code5Systems.FormMaster.Business.FormController.AutoFillType.None:
					this.trAutoFillFrom.Visible = false;
					this.trAutoFillParam.Visible = false;
					this.trDefaultText.Visible = true;
					break;
					
				case Code5Systems.FormMaster.Business.FormController.AutoFillType.Membership:
					this.trAutoFillFrom.Visible = true;
					this.trAutoFillParam.Visible = false;
					this.trDefaultText.Visible = false;
					this.lstAutoFillFieldFrom.Visible = true;
					this.lstServerVariables.Visible = false;
					break;
					
				case Code5Systems.FormMaster.Business.FormController.AutoFillType.Session:
				case Code5Systems.FormMaster.Business.FormController.AutoFillType.Url:
				case Code5Systems.FormMaster.Business.FormController.AutoFillType.Cookie:
					this.trAutoFillFrom.Visible = false;
					this.trAutoFillParam.Visible = true;
					this.trDefaultText.Visible = false;
					break;
					
				case Code5Systems.FormMaster.Business.FormController.AutoFillType.FormQuery:
					this.trAutoFillFrom.Visible = false;
					this.trAutoFillParam.Visible = true;
					this.trDefaultText.Visible = false;
					break;
					
				case Code5Systems.FormMaster.Business.FormController.AutoFillType.FieldQuery:
					this.trAutoFillFrom.Visible = false;
					this.trAutoFillParam.Visible = true;
					this.trDefaultText.Visible = false;
					break;
					
				case Code5Systems.FormMaster.Business.FormController.AutoFillType.ServerVariables:
					this.trAutoFillFrom.Visible = true;
					this.trAutoFillParam.Visible = false;
					this.trDefaultText.Visible = false;
					this.lstAutoFillFieldFrom.Visible = false;
					this.lstServerVariables.Visible = true;
					break;
					
			}
			
			if (this.trDefaultText.Visible)
			{
				Code5Systems.FormMaster.Business.ControlType formControlType = int.Parse(this.lstType.SelectedValue).ToEnum <ControlType>();
				
				//We set the visibility status above but the ControlType has the last say
				if (formControlType != Code5Systems.FormMaster.Business.ControlType.TextBox && formControlType != Code5Systems.FormMaster.Business.ControlType.HiddenField)
				{
					this.trDefaultText.Visible = false;
				}
			}
			
		}
		
		protected void lstButtonType_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			
			if (!(ViewState["ControlLocation"] == null))
			{
				if (int.Parse(this.lstButtonType.SelectedValue).ToEnum <Code5Systems.FormMaster.Business.ButtonType>() == Code5Systems.FormMaster.Business.ButtonType.ImageButton)
				{
					this.trButtonImage.Visible = true;
					
					Code5Systems.FormMaster.Business.FormControlInfo objFormControlInfo = default(Code5Systems.FormMaster.Business.FormControlInfo);
					
					this.imgButton.Visible = false;
					
					//Get the information on this control in the current working row and column
					objFormControlInfo = this.GetFormControlInfo((oCtlLoc.Row).ToString(), (oCtlLoc.Col).ToString());
					if (objFormControlInfo != null)
					{
						if (objFormControlInfo.ButtonImage != string.Empty)
						{
							
							FileController oFileCtl = new FileController();
							DotNetNuke.Services.FileSystem.FileInfo oFileInfo = oFileCtl.GetFileById(int.Parse(objFormControlInfo.ButtonImage), PortalId);
							if (oFileInfo != null)
							{
								this.imgButton.ImageUrl = PortalSettings.HomeDirectory + oFileInfo.Folder + oFileInfo.FileName;
								this.imgButton.AlternateText = objFormControlInfo.Label;
								this.imgButton.Visible = true;
								
								this.lblCurrentFile.Text = Localization.GetString("CurrentFile.Text", this.LocalResourceFile);
								this.lblCurrentFile.Text += oFileInfo.Folder + oFileInfo.FileName;
							}
							
						}
					}
				}
				else
				{
					this.trButtonImage.Visible = false;
				}
			}
		}
		
		protected void cmdTestQuery_Click(object sender, System.EventArgs e)
		{
			
			this.TestQuery(this.txtFieldQuery.Text);
			
		}
		
		protected void cmdTestCascadingQuery_Click(object sender, System.EventArgs e)
		{
			
			this.TestQuery(this.txtCascadingFieldQuery.Text);
			
		}
		
		protected void chkCascadingList_CheckedChanged(object sender, System.EventArgs e)
		{
			this.lblCascadeParent.Visible = this.chkCascadingList.Checked;
			this.lstCascadeParent.Visible = this.chkCascadingList.Checked;
			this.lblPromptText.Visible = this.chkCascadingList.Checked;
			this.txtPromptText.Visible = this.chkCascadingList.Checked;
			this.lblNotFoundText.Visible = this.chkCascadingList.Checked;
			this.txtNotFoundText.Visible = this.chkCascadingList.Checked;
			this.lblSelectText.Visible = this.chkCascadingList.Checked;
			this.txtSelectText.Visible = this.chkCascadingList.Checked;
			this.dgListItems.Columns[0].Visible = this.chkCascadingList.Checked;
			this.rfvCascadeParent.Enabled = this.chkCascadingList.Checked;
			this.rfvPromptText.Enabled = this.chkCascadingList.Checked;
			this.rfvNotFoundText.Enabled = this.chkCascadingList.Checked;
			this.rfvSelectText.Enabled = this.chkCascadingList.Checked;
			if (this.chkCascadingList.Checked && this.lstCascadeParent.SelectedIndex == 0)
			{
				this.dgListItems.Enabled = false;
			}
			else
			{
				this.dgListItems.Enabled = true;
			}
			// TODO: Populate Cascade Parent List
		}
		
		protected void lstCascadeParent_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (this.chkCascadingList.Checked && this.lstCascadeParent.SelectedIndex == 0)
			{
				this.dgListItems.Enabled = false;
				this.rfvCascadeParent.Validate();
			}
			else
			{
				int footerIndex = System.Convert.ToInt32(this.dgListItems.Controls[0].Controls.Count - 1);
				Code5Systems.FormMaster.Business.FormControlInfo parent = this.objFormCtl.GetFormControlInfo(this.lstCascadeParent.SelectedValue);
				DropDownList parentItems = (DropDownList) (this.dgListItems.Controls[0].Controls[footerIndex].FindControl("lstNewCategory"));
				if (parentItems != null)
				{
					parentItems.DataSource = parent.ListItems;
					parentItems.DataBind();
				}
				this.dgListItems.Enabled = true;
			}
		}
		
#endregion
		
#region dgListItems Event Handlers
		
		//List Items Page Changed Event
		protected void dgListItems_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			if (!(ViewState["ControlLocation"] == null))
			{
				dgListItems.CurrentPageIndex = e.NewPageIndex;
				this.BindControlForm();
			}
		}
		
		//List Items Item Command Event
		protected void dgListItems_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			Page.Validate();
			if (Page.IsValid)
			{
				Code5Systems.FormMaster.Business.FormControlInfo objFormControlInfo = default(Code5Systems.FormMaster.Business.FormControlInfo);
				string strCommandName = string.Empty;
				try
				{
					string strType = e.CommandSource.GetType().ToString();
					switch (strType)
					{
						case "System.Web.UI.WebControls.ImageButton":
							strCommandName = ((ImageButton) e.CommandSource).CommandName;
							break;
						case "System.Web.UI.WebControls.LinkButton":
							strCommandName = ((LinkButton) e.CommandSource).CommandName;
							break;
					}
					
					//Save any changes the user has made to the form
					if (!(ViewState["ControlLocation"] == null))
					{
						objFormControlInfo = this.GetFormControlInfo((oCtlLoc.Row).ToString(), (oCtlLoc.Col).ToString());
						
						if (objFormControlInfo == null)
						{
							objFormControlInfo = objFormCtl.AddControl(this.xDoc, int.Parse(lstType.SelectedItem.Value).ToEnum<ControlType>(), oCtlLoc.Row.ToString(), oCtlLoc.Col.ToString());
						}
						
						FillFormControlInfo(objFormControlInfo);
						objFormCtl.UpdateControlValues(this.xDoc, objFormControlInfo);
						SaveFormDefinition();
						
					}
					else
					{
						DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, Localization.GetString("ControlLocation.Error"), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
						return;
					}
					
					switch (strCommandName)
					{
						case "Edit":
							if (!(ViewState["ControlLocation"] == null))
							{
								if (objFormControlInfo != null)
								{
									this.dgListItems.EditItemIndex = e.Item.ItemIndex;
									this.dgListItems.ShowFooter = false;
									this.cmdControlUpdate.Enabled = false;
									this.cmdCancel.Enabled = false;
									this.lstType.Enabled = false;
								}
							}
							break;
						case "Delete":
							int iIndex_1 = e.Item.ItemIndex;
							
							if (objFormControlInfo != null)
							{
								
								objFormCtl.DeleteListItem(this.xDoc, objFormControlInfo, iIndex_1);
								
								SaveFormDefinition();
							}
							
							DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
							break;
							
						case "DeleteAll":
							
							if (objFormControlInfo != null)
							{
								objFormCtl.DeleteAllListItems(this.xDoc, objFormControlInfo);
								SaveFormDefinition();
							}
							
							DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
							break;
							
						case "Update":
							int iIndex = e.Item.ItemIndex;
							
							string itemCategory_1 = ((DropDownList) (e.Item.Cells[0].Controls[1])).SelectedValue;
							string itemText_1 = ((TextBox) (e.Item.Cells[1].Controls[1])).Text;
							string itemValue_1 = ((TextBox) (e.Item.Cells[2].Controls[1])).Text;
							bool itemSelected_1 = ((CheckBox) (e.Item.Cells[3].Controls[1])).Checked;
							
							if (itemText_1 != string.Empty)
							{
								if (itemSelected_1 && objFormControlInfo.Type == Code5Systems.FormMaster.Business.ControlType.DropDownList)
								{
									
									if (!ValidateDropdownList(objFormControlInfo))
									{
										break;
									}
								}
								
								if (objFormControlInfo != null)
								{
									objFormCtl.UpdateListItem(this.xDoc, objFormControlInfo, itemCategory_1, itemText_1, itemValue_1, itemSelected_1, iIndex);
									SaveFormDefinition();
								}
								
								this.dgListItems.EditItemIndex = -1;
								this.dgListItems.ShowFooter = true;
								this.cmdControlUpdate.Enabled = true;
								this.cmdCancel.Enabled = true;
								this.lstType.Enabled = true;
								
								DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
							}
							else
							{
								DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, Localization.GetString("ListItemDataErrror", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
							}
							break;
							
						case "Insert":
							
							string itemCategory = ((DropDownList) (e.Item.Cells[0].Controls[1])).SelectedValue;
							string itemText = ((TextBox) (e.Item.Cells[1].Controls[1])).Text;
							string itemValue = ((TextBox) (e.Item.Cells[2].Controls[1])).Text;
							bool itemSelected = ((CheckBox) (e.Item.Cells[3].Controls[1])).Checked;
							
							if (itemText != string.Empty)
							{
								if (itemValue == string.Empty)
								{
									itemValue = itemText;
								}
								
								if (itemSelected && objFormControlInfo.Type == Code5Systems.FormMaster.Business.ControlType.DropDownList)
								{
									
									if (!ValidateDropdownList(objFormControlInfo))
									{
										break;
									}
									
								}
								
								if (objFormControlInfo != null)
								{
									objFormCtl.AddListItem(this.xDoc, objFormControlInfo, itemCategory, itemText, itemValue, itemSelected);
									SaveFormDefinition();
								}
								
								DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
							}
							else
							{
								DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, Localization.GetString("ListItemDataErrror", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
							}
							break;
							
						case "Cancel":
							this.dgListItems.ShowFooter = true;
							this.dgListItems.EditItemIndex = -1;
							this.cmdControlUpdate.Enabled = true;
							this.cmdCancel.Enabled = true;
							this.lstType.Enabled = true;
							break;
							
						case "up":
							if (!(ViewState["ControlLocation"] == null))
							{
								if (ChangeListItemPosition(true, e.Item.ItemIndex))
								{
									SaveFormDefinition();
									DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
								}
								else
								{
									DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, Localization.GetString("MoveListItemErrror", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
								}
							}
							break;
							
						case "down":
							if (!(ViewState["ControlLocation"] == null))
							{
								if (ChangeListItemPosition(false, e.Item.ItemIndex))
								{
									SaveFormDefinition();
									DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
								}
								else
								{
									DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, Localization.GetString("MoveListItemErrror", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
								}
							}
							break;
					}
					
					if (!(ViewState["ControlLocation"] == null))
					{
						bFormChange = true;
						this.BindControlForm();
					}
					
				}
				catch (Exception exc)
				{
					Exceptions.ProcessModuleLoadException(this.LoaderCtl, exc);
				}
			}
		}
		
		//List Items DataBound Event to add the Confirm Delete Attribute.
		protected void dgListItems_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			
			WebControl oWebControl = (WebControl) (e.Item.FindControl("cmdDeleteListItem"));
			if (oWebControl != null)
			{
				oWebControl.Attributes.Add("onclick", "javascript:return confirm(\'" + Localization.GetString("DeleteItemMessage", LocalResourceFile) + "\');");
			}
			
			oWebControl = (WebControl) (e.Item.FindControl("cmdDeleteAllListItems"));
			if (oWebControl != null)
			{
				oWebControl.Attributes.Add("onclick", "javascript:return confirm(\'" + Localization.GetString("DeleteAllItemsMessage", LocalResourceFile) + "\');");
			}
			
			if (_objFormControlInfo.IsChildList)
			{
				Code5Systems.FormMaster.Business.FormControlInfo parent = this.objFormCtl.GetFormControlInfo(_objFormControlInfo.ParentControlID);
				if (this.dgListItems.EditItemIndex > -1 && this.dgListItems.EditItemIndex == e.Item.ItemIndex)
				{
					DropDownList parentItems = (DropDownList) (e.Item.FindControl("lstCategory"));
					if (parentItems != null)
					{
						parentItems.DataSource = parent.ListItems;
						parentItems.DataBind();
						Code5Systems.FormMaster.Business.FMListItem thisItem = (Code5Systems.FormMaster.Business.FMListItem) e.Item.DataItem;
						parentItems.SelectedIndex = parentItems.Items.IndexOf(parentItems.Items.FindByValue(thisItem.Category));
					}
				}
			}
			
			if (e.Item.ItemType == ListItemType.Footer)
			{
				if (_objFormControlInfo.CascadingList)
				{
					Code5Systems.FormMaster.Business.FormControlInfo parent = this.objFormCtl.GetFormControlInfo(_objFormControlInfo.ParentControlID);
					DropDownList newParentItems = (DropDownList) (e.Item.FindControl("lstNewCategory"));
					if (newParentItems != null)
					{
						newParentItems.DataSource = parent.ListItems;
						newParentItems.DataBind();
					}
				}
			}
		}
		
		
#endregion
		
#region dgValidators Event Handlers
		
		//Validators DataGrid ItemCommand Event Handler
		protected void dgValidators_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			Page.Validate();
			if (Page.IsValid)
			{
				Code5Systems.FormMaster.Business.FormControlInfo objFormControlInfo = default(Code5Systems.FormMaster.Business.FormControlInfo);
				string strCommandName = default(string);
				try
				{
					//Save any changes the user has made to the form
					if (!(ViewState["ControlLocation"] == null))
					{
						objFormControlInfo = this.GetFormControlInfo((oCtlLoc.Row).ToString(), (oCtlLoc.Col).ToString());
						
						if (objFormControlInfo == null)
						{
							objFormControlInfo = objFormCtl.AddControl(this.xDoc, int.Parse(lstType.SelectedItem.Value).ToEnum<ControlType>(), (oCtlLoc.Row).ToString(), (oCtlLoc.Col).ToString());
						}
						
						FillFormControlInfo(objFormControlInfo);
						objFormCtl.UpdateControlValues(this.xDoc, objFormControlInfo);
						SaveFormDefinition();
						
					}
					else
					{
						DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, Localization.GetString("ControlLocation.Error"), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
						return;
					}
					
					strCommandName = ((LinkButton) e.CommandSource).CommandName;
					
					switch (strCommandName)
					{
						case "Edit":
							string strValType_1 = string.Empty;
							Code5Systems.FormMaster.Business.ControlType type_1 = default(Code5Systems.FormMaster.Business.ControlType);
							
							Label oLabel_1 = (Label) (e.Item.FindControl("ValTypeValue"));
							if (oLabel_1 != null)
							{
								strValType_1 = oLabel_1.Text;
							}
							
							if (strValType_1 == ControlType.RequiredFieldValidator.ToString())
							{
								type_1 = Code5Systems.FormMaster.Business.ControlType.RequiredFieldValidator;
							}
							else if (strValType_1 == ControlType.RegularExpressionValidator.ToString())
							{
								type_1 = Code5Systems.FormMaster.Business.ControlType.RegularExpressionValidator;
							}
							else if (strValType_1 == ControlType.CompareValidator.ToString())
							{
								type_1 = Code5Systems.FormMaster.Business.ControlType.CompareValidator;
							}
							else if (strValType_1 == ControlType.ChkListValidator.ToString())
							{
								type_1 = Code5Systems.FormMaster.Business.ControlType.ChkListValidator;
							}
							
							//Get the Validator we want to Edit into the Class Scope FormControlInfo Object
							if (objFormControlInfo != null)
							{
								objCurValidatorControlInfo = objFormCtl.GetValidatorControlInfo(this.xDoc, objFormControlInfo, type_1);
							}
							
							this.dgValidators.EditItemIndex = e.Item.ItemIndex;
							this.dgValidators.ShowFooter = false;
							this.cmdControlUpdate.Enabled = false;
							this.cmdCancel.Enabled = false;
							break;
							
						case "Delete":
							Label oLabel_2 = default(Label);
							string strValType_2 = "";
							Code5Systems.FormMaster.Business.ControlType type_2 = default(Code5Systems.FormMaster.Business.ControlType);
							
							oLabel_2 = (Label) (e.Item.FindControl("ValTypeValue"));
							if (oLabel_2 != null)
							{
								strValType_2 = oLabel_2.Text;
							}
							
							if (strValType_2 == ControlType.RequiredFieldValidator.ToString())
							{
								type_2 = Code5Systems.FormMaster.Business.ControlType.RequiredFieldValidator;
							}
							else if (strValType_2 == ControlType.RegularExpressionValidator.ToString())
							{
								type_2 = Code5Systems.FormMaster.Business.ControlType.RegularExpressionValidator;
							}
							else if (strValType_2 == ControlType.CompareValidator.ToString())
							{
								type_2 = Code5Systems.FormMaster.Business.ControlType.CompareValidator;
							}
							else if (strValType_2 == ControlType.ChkListValidator.ToString())
							{
								type_2 = Code5Systems.FormMaster.Business.ControlType.ChkListValidator;
							}
							
							if (objFormControlInfo != null)
							{
								objFormCtl.DeleteValidatorControl(this.xDoc, objFormControlInfo, type_2);
								SaveFormDefinition();
							}
							
							DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
							break;
							
						case "Update":
							Code5Systems.FormMaster.Business.FormControlInfo objValidatorControlInfo = null;
							Label oLabel = default(Label);
							string strValType = "";
							Code5Systems.FormMaster.Business.ControlType type_3 = default(Code5Systems.FormMaster.Business.ControlType);
							WebControl oWebControl = default(WebControl);
							
							oLabel = (Label) (e.Item.FindControl("EditValTypeValue"));
							if (oLabel != null)
							{
								strValType = oLabel.Text;
							}
							
							if (strValType == ControlType.RequiredFieldValidator.ToString())
							{
								type_3 = Code5Systems.FormMaster.Business.ControlType.RequiredFieldValidator;
							}
							else if (strValType == ControlType.RegularExpressionValidator.ToString())
							{
								type_3 = Code5Systems.FormMaster.Business.ControlType.RegularExpressionValidator;
							}
							else if (strValType == ControlType.CompareValidator.ToString())
							{
								type_3 = Code5Systems.FormMaster.Business.ControlType.CompareValidator;
							}
							else if (strValType == ControlType.ChkListValidator.ToString())
							{
								type_3 = Code5Systems.FormMaster.Business.ControlType.ChkListValidator;
							}
							
							//Get the Validator we want to Update into the Local Scope FormControlInfo Object
							if (objFormControlInfo != null)
							{
								objValidatorControlInfo = objFormCtl.GetValidatorControlInfo(this.xDoc, objFormControlInfo, type_3);
							}
							
							if (objValidatorControlInfo != null)
							{
								
								oWebControl = (WebControl) (e.Item.FindControl("EditValErrMsgValue"));
								if (oWebControl != null)
								{
									objValidatorControlInfo.ErrorMessage = ((TextBox) oWebControl).Text;
									if (objValidatorControlInfo.ErrorMessage == string.Empty)
									{
										objValidatorControlInfo.ErrorMessage = "A value is required";
									}
								}
								
								oWebControl = (WebControl) (e.Item.FindControl("EditClassValErrValue"));
								if (oWebControl != null)
								{
									objValidatorControlInfo.CssClass = ((TextBox) oWebControl).Text;
								}
								
								oWebControl = (WebControl) (e.Item.FindControl("EditSetFocusOnError"));
								if (oWebControl != null)
								{
									objValidatorControlInfo.SetFocusOnError = ((CheckBox) oWebControl).Checked;
								}
								
								switch (objValidatorControlInfo.Type)
								{
									case Code5Systems.FormMaster.Business.ControlType.RequiredFieldValidator:
										oWebControl = (WebControl) (e.Item.FindControl("EditInitValValue"));
										if (oWebControl != null)
										{
											objValidatorControlInfo.InitialValue = ((TextBox) oWebControl).Text;
										}
										break;
										
									case Code5Systems.FormMaster.Business.ControlType.ChkListValidator:
										oWebControl = (WebControl) (e.Item.FindControl("EditMinCheckedValue"));
										if (oWebControl != null)
										{
											objValidatorControlInfo.MinChecked = int.Parse(((TextBox) oWebControl).Text);
										}
										
										oWebControl = (WebControl) (e.Item.FindControl("EditMaxCheckedValue"));
										if (oWebControl != null)
										{
											objValidatorControlInfo.MaxChecked = int.Parse(((TextBox) oWebControl).Text);
										}
										break;
										
									case Code5Systems.FormMaster.Business.ControlType.RegularExpressionValidator:
										oWebControl = (WebControl) (e.Item.FindControl("EditLstRegEx"));
										if (oWebControl != null)
										{
											if (((DropDownList) oWebControl).SelectedValue != "0")
											{
												objValidatorControlInfo.ValidationExpression = ((DropDownList) oWebControl).SelectedValue;
												break;
											}
										}
										
										oWebControl = (WebControl) (e.Item.FindControl("EditRegexValue"));
										if (oWebControl != null)
										{
											try
											{
												System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(((TextBox) oWebControl).Text);
											}
											catch (Exception ex)
											{
												DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, ex.Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
												return;
											}
											objValidatorControlInfo.ValidationExpression = ((TextBox) oWebControl).Text;
										}
										break;
										
									case Code5Systems.FormMaster.Business.ControlType.CompareValidator:
										oWebControl = (WebControl) (e.Item.FindControl("EditLstControlToCompare"));
										if (oWebControl != null)
										{
											if (((DropDownList) oWebControl).SelectedValue != "0")
											{
												objValidatorControlInfo.ControlToCompare = ((DropDownList) oWebControl).SelectedValue;
											}
											else
											{
												objValidatorControlInfo.ControlToCompare = "";
											}
										}
										
										oWebControl = (WebControl) (e.Item.FindControl("EditOperatorValue"));
										if (oWebControl != null)
										{
											objValidatorControlInfo.Operator = int.Parse(((DropDownList) oWebControl).SelectedValue).ToEnum<ValidationCompareOperator>();
										}
										
										oWebControl = (WebControl) (e.Item.FindControl("EditValDataTypeValue"));
										if (oWebControl != null)
										{
											objValidatorControlInfo.ValDataType = int.Parse(((DropDownList) oWebControl).SelectedValue).ToEnum<ValidationDataType>();
										}
										
										oWebControl = (WebControl) (e.Item.FindControl("EditValueToCompareValue"));
										if (oWebControl != null)
										{
											objValidatorControlInfo.ValueToCompare = ((TextBox) oWebControl).Text;
										}
										
										//Verify that the Value entered for the ValueToCompare is valid
										//for the Data Type Selected.
										if (objValidatorControlInfo.ValueToCompare != string.Empty)
										{
											try
											{
												switch (objValidatorControlInfo.ValDataType)
												{
													case ValidationDataType.Double:
														double dblValue = double.Parse(objValidatorControlInfo.ValueToCompare);
														break;
													case ValidationDataType.Currency:
														decimal decValue = decimal.Parse(objValidatorControlInfo.ValueToCompare);
														break;
													case ValidationDataType.Date:
														DateTime dateValue = DateTime.Parse(objValidatorControlInfo.ValueToCompare);
														break;
													case ValidationDataType.Integer:
														int intValue = int.Parse(objValidatorControlInfo.ValueToCompare);
														break;
												}
											}
											catch (Exception ex)
											{
												throw (new ArgumentException(ex.Message));
											}
										}
										break;
								}
								
								//Update the changes in the XmlDocument
								objFormCtl.UpdateControlValues(this.xDoc, objValidatorControlInfo);
								
								//Save the Changes
								this.SaveFormDefinition();
								
								this.dgValidators.EditItemIndex = -1;
								this.dgValidators.ShowFooter = true;
								this.cmdControlUpdate.Enabled = true;
								this.cmdCancel.Enabled = true;
								
								DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
							}
							else
							{
								DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, Localization.GetString("OperationFail.ErrorMessage", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
							}
							break;
							
							
						case "Insert":
							
							DropDownList lstControlType = (DropDownList) (e.Item.Cells[0].FindControl("lstValidatorType"));
							
							if (lstControlType != null)
							{
								Code5Systems.FormMaster.Business.ControlType type = (Code5Systems.FormMaster.Business.ControlType) (int.Parse(lstControlType.SelectedValue));
								
								if (objFormControlInfo != null)
								{
									objFormControlInfo.Label = this.txtLabel.Text;
									objFormControlInfo.FieldName = this.txtFieldName.Text;
									
									//Get the New Validator into the Class Scope FormControlInfo Object
									objCurValidatorControlInfo = objFormCtl.AddValidatorControl(this.xDoc, objFormControlInfo, type);
									
									if (objCurValidatorControlInfo != null)
									{
										SaveFormDefinition();
										this.dgValidators.EditItemIndex = this.dgValidators.Items.Count;
										this.cmdControlUpdate.Enabled = false;
										this.cmdCancel.Enabled = false;
										this.dgValidators.ShowFooter = false;
									}
									else
									{
										DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, Localization.GetString("DuplicateValidators.ErrorMessage", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
									}
								}
							}
							break;
							
						case "Cancel":
							this.dgValidators.ShowFooter = true;
							this.dgValidators.EditItemIndex = -1;
							this.cmdControlUpdate.Enabled = true;
							this.cmdCancel.Enabled = true;
							break;
					}
					
					//bFormChange = True
					
					this.BindControlForm();
				}
				catch (ArgumentException)
				{
					DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, Localization.GetString("InvalidValueToCompareValue.ErrorMessage", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
				}
				catch (Exception exc)
				{
					Exceptions.ProcessModuleLoadException(this.LoaderCtl, exc);
				}
			}
		}
		
		//Validators Data Grid ItemDataBound Event Handler.
		protected void dgValidators_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			string strValType = "";
			string strPrefix = "";
			Label oLabel = default(Label);
			WebControl oWebControl = default(WebControl);
			
			//If we are in edit mode, set the prefix variable to "Edit" to match the naming convention in the
			//ascx page for the various controls in the Data Grid TemplateColumns.
			if (this.dgValidators.EditItemIndex > -1 && this.dgValidators.EditItemIndex == e.Item.ItemIndex)
			{
				strPrefix = "Edit";
			}
			
			//Determine the Type of Validator
			oLabel = (Label) (e.Item.FindControl(strPrefix + "ValTypeValue"));
			if (oLabel != null)
			{
				strValType = oLabel.Text;
			}
			
			if (((Code5Systems.FormMaster.Business.ControlType) (byte.Parse(lstType.SelectedValue))) == Code5Systems.FormMaster.Business.ControlType.CheckBoxList)
			{
				DropDownList lstValidatorType = (DropDownList) (e.Item.FindControl("lstValidatorType"));
				if (lstValidatorType != null)
				{
					lstValidatorType.Items.Clear();
					lstValidatorType.Items.Add(new ListItem(Localization.GetString("CheckListValidator.Text", this.LocalResourceFile), "14"));
				}
			}
			
			oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "SetFocusOnError"));
			if (oWebControl != null)
			{
				((CheckBox) oWebControl).Checked = objCurValidatorControlInfo.SetFocusOnError;
			}
			
			//Bind the data, and set visibilty according to the Validator Type
			if (strValType == ControlType.RequiredFieldValidator.ToString())
			{
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "InitVal"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "InitValValue"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
			}
			else if (strValType == ControlType.ChkListValidator.ToString())
			{
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "MinChecked"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "MinCheckedValue"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "MaxChecked"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "MaxCheckedValue"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				RangeValidator oMinRangeValidator = (RangeValidator) (e.Item.FindControl(strPrefix + "rvMinChecked"));
				if (oMinRangeValidator != null)
				{
					oMinRangeValidator.MinimumValue = "1";
					oMinRangeValidator.MaximumValue = (this.dgListItems.Items.Count).ToString();
					oMinRangeValidator.ErrorMessage = string.Format(Localization.GetString("ChkLstValidationMsg.Text", this.LocalResourceFile), 1, oMinRangeValidator.MaximumValue);
					oMinRangeValidator.Enabled = true;
				}
				
				RangeValidator oMaxRangeValidator = (RangeValidator) (e.Item.FindControl(strPrefix + "rvMaxChecked"));
				if (oMaxRangeValidator != null)
				{
					oMaxRangeValidator.MinimumValue = "0";
					oMaxRangeValidator.MaximumValue = oMinRangeValidator.MaximumValue;
					oMaxRangeValidator.ErrorMessage = string.Format(Localization.GetString("ChkLstValidationMsg.Text", this.LocalResourceFile), 0, oMinRangeValidator.MaximumValue);
					oMaxRangeValidator.Enabled = true;
				}
			}
			else if (strValType == ControlType.RegularExpressionValidator.ToString())
			{
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "Regex"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "LstRegEx"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "RegexValue"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
			}
			else if (strValType == ControlType.CompareValidator.ToString())
			{
				// Get the ControlToCompare Name
				oLabel = (Label)(e.Item.FindControl(strPrefix + "LblControlToCompareValue"));
				if (oLabel != null)
				{
					oLabel.Text = this.GetControlName(oLabel.Text);
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "LblControlToCompare"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "LblControlToCompareValue"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "LstControlToCompare"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
					DropDownList olstOperator = (DropDownList) oWebControl;
					
					BindDynamicControls(olstOperator, false);
					
					if (objCurValidatorControlInfo != null)
					{
						ListItem oListItem = default(ListItem);
						
						//We don't want the current control in the ControlToCompare List
						oListItem = olstOperator.Items.FindByValue(objCurValidatorControlInfo.ControlToValidate);
						if (oListItem != null)
						{
							olstOperator.Items.Remove(oListItem);
						}
						
						oListItem = new ListItem("<< None >>", "0");
						olstOperator.Items.Insert(0, oListItem);
						
						olstOperator.SelectedIndex = olstOperator.Items.IndexOf(olstOperator.Items.FindByValue(objCurValidatorControlInfo.ControlToCompare));
					}
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "Operator"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "OperatorValue"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
					if (objCurValidatorControlInfo != null)
					{
						if (objCurValidatorControlInfo.Type == Code5Systems.FormMaster.Business.ControlType.CompareValidator)
						{
							DropDownList olstOperator = (DropDownList) oWebControl;
							int selIndex = Convert.ToInt32(objCurValidatorControlInfo.Operator);
							olstOperator.SelectedIndex = olstOperator.Items.IndexOf(olstOperator.Items.FindByValue(selIndex.ToString()));
						}
					}
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "ValDataType"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "ValDataTypeValue"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
					if (objCurValidatorControlInfo != null)
					{
						if (objCurValidatorControlInfo.Type == Code5Systems.FormMaster.Business.ControlType.CompareValidator)
						{
							DropDownList olstValDataType = (DropDownList) oWebControl;
							int selIndex = Convert.ToInt32(objCurValidatorControlInfo.ValDataType);
							olstValDataType.SelectedIndex = olstValDataType.Items.IndexOf(olstValDataType.Items.FindByValue(selIndex.ToString()));
						}
					}
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "ValueToCompare"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
				
				oWebControl = (WebControl) (e.Item.FindControl(strPrefix + "ValueToCompareValue"));
				if (oWebControl != null)
				{
					oWebControl.Visible = true;
				}
			}
			
			//Add the Confirmation Dialog for the Delete Command.
			oWebControl = (WebControl) (e.Item.FindControl("cmdDeleteVal"));
			if (oWebControl != null)
			{
				if (this.dgValidators.EditItemIndex > -1)
				{
					oWebControl.Enabled = false;
				}
				else
				{
					oWebControl.Attributes.Add("onclick", "javascript:return confirm(\'" + Localization.GetString("DeleteItemMessage", LocalResourceFile) + "\');");
				}
			}
			
		}

		protected void dgValidators_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Footer)
			{
				e.Item.Cells[0].ColumnSpan = 3;
				e.Item.Cells.RemoveAt(2);
				e.Item.Cells.RemoveAt(1);
			}
		}
		
#endregion
		
		
		
		
		
	}
}

